/*test case generator*/
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iostream>
#include <fstream>

#define int long long
const int miny = -10000;
const int maxy = +10000;
const int maxn = 1111111 + 111;
const unsigned SEED = 114514;
int n,y[maxn],ans;

void calculate_ans(){
	std::sort(y+1, y+1+n);
	int median_y = y[n/2+1];
	printf("[debug]median for this case: %lld \n", median_y);
	
	ans = 0;
	for ( int i=1; i<=n; i++ )
		ans += abs(y[i]-median_y);
		
	printf("[debug]ans for this case: %lld \n", ans);
}

signed main() {
	//test case 1 input: same as sample in description in problem
	FILE *file = fopen("D:/AAAstuffsAAA/jmu-algo/exp01/p1/1-1_test_cases/1.in", "w");
	if (file == NULL) {
		printf("无法创建文件 1.in\n");
		return 1;
	}
	
	fprintf(file,"4\n");
	fprintf(file,"-1 1\n");
	fprintf(file,"-2 2\n");
	fprintf(file,"5 -1\n");
	fprintf(file,"-3 7\n");
	
	fclose(file);  // 关闭文件
	printf("文件1.in生成成功\n");

	/*-----------------------------------------------------------------------------------------*/
	
	//test case 1 output: same as sample in description in problem
	file = fopen("D:/AAAstuffsAAA/jmu-algo/exp01/p1/1-1_test_cases/1.out", "w");
	if (file == NULL) {
		printf("无法创建文件 1.out\n");
		return 1;
	}
	
	fprintf(file,"9\n");
	
	fclose(file);  // 关闭文件
	printf("文件1.out生成成功\n");
	
	/*-----------------------------------------------------------------------------------------*/
	//test case 2 input: 11 distinct numbers, to guarantee most basic corectness
	file = fopen("D:/AAAstuffsAAA/jmu-algo/exp01/p1/1-1_test_cases/2.in", "w");
	if (file == NULL) {
		printf("无法创建文件 2.in\n");
		return 1;
	}
	
	//横坐标无所谓 随便写就行了， 因为横坐标和结果无关
	fprintf(file,"11\n");
	fprintf(file,"1 4\n");
	fprintf(file,"2 8\n");
	fprintf(file,"3 3\n");
	fprintf(file,"4 7\n");
	fprintf(file,"5 1\n");
	fprintf(file,"6 9\n");
	fprintf(file,"7 11\n");
	fprintf(file,"8 2\n");
	fprintf(file,"9 5\n");
	fprintf(file,"10 10\n");
	fprintf(file,"11 6\n");
	
	fclose(file);  // 关闭文件
	printf("文件2.in生成成功\n");
	
	/*-----------------------------------------------------------------------------------------*/
	//test case 2 output: 11 distinct numbers, to guarantee most basic corectness
	file = fopen("D:/AAAstuffsAAA/jmu-algo/exp01/p1/1-1_test_cases/2.out", "w");
	if (file == NULL) {
		printf("无法创建文件 2.out\n");
		return 1;
	}
	
	//横坐标无所谓 随便写就行了， 因为横坐标和结果无关
	fprintf(file,"30\n");
	
	fclose(file);  // 关闭文件
	printf("文件2.out生成成功\n");
	
	/*-----------------------------------------------------------------------------------------*/
	//test case 3 input: 1e3 random numbers
	n = 1111;
	srand(SEED);
	for ( int i=1; i<=n; i++ )
		y[i] = rand()%(maxy-miny+1) + miny;
	
	file = fopen("D:/AAAstuffsAAA/jmu-algo/exp01/p1/1-1_test_cases/3.in", "w");
	if (file == NULL) {
		printf("无法创建文件 3.in\n");
		return 1;
	}
	
	fprintf(file,"%lld\n", n);
	for ( int i=1; i<=n; i++ ){
		fprintf(file,"%lld %lld\n", i, y[i] );
	}

	fclose(file);  // 关闭文件
	printf("文件3.in生成成功\n");
	
	/*-----------------------------------------------------------------------------------------*/
	//test case 3 output: 1e3 random numbers
	file = fopen("D:/AAAstuffsAAA/jmu-algo/exp01/p1/1-1_test_cases/3.out", "w");
	if (file == NULL) {
		printf("无法创建文件 3.out\n");
		return 1;
	}
	
	calculate_ans();
	fprintf(file,"%lld\n", ans);
	
	fclose(file);  // 关闭文件
	printf("文件3.out生成成功\n");
	/*-----------------------------------------------------------------------------------------*/
	//test case 4 input: 1e4 random numbers
	n = 11111;
	//如果在同一个C文件内，通过srand()函数设置了种子值，并且后续没有再次调用srand()函数改变种子值，那么种子值将保持不变。
	for ( int i=1; i<=n; i++ )
		y[i] = rand()%(maxy-miny+1) + miny;
	
	file = fopen("D:/AAAstuffsAAA/jmu-algo/exp01/p1/1-1_test_cases/4.in", "w");
	if (file == NULL) {
		printf("无法创建文件 4.in\n");
		return 1;
	}
	
	fprintf(file,"%lld\n", n);
	for ( int i=1; i<=n; i++ ){
		fprintf(file,"%lld %lld\n", i, y[i] );
	}
	
	fclose(file);  // 关闭文件
	printf("文件4.in生成成功\n");
	
	/*-----------------------------------------------------------------------------------------*/
	//test case 4 output: 1e4 random numbers
	file = fopen("D:/AAAstuffsAAA/jmu-algo/exp01/p1/1-1_test_cases/4.out", "w");
	if (file == NULL) {
		printf("无法创建文件 4.out\n");
		return 1;
	}
	
	calculate_ans();
	fprintf(file,"%lld\n", ans);
	
	fclose(file);  // 关闭文件
	printf("文件4.out生成成功\n");
	/*-----------------------------------------------------------------------------------------*/
	//test case 5 input: 1e5 random numbers
	n = 111111;
	for ( int i=1; i<=n; i++ )
		y[i] = rand()%(maxy-miny+1) + miny;
	
	file = fopen("D:/AAAstuffsAAA/jmu-algo/exp01/p1/1-1_test_cases/5.in", "w");
	if (file == NULL) {
		printf("无法创建文件 5.in\n");
		return 1;
	}
	
	fprintf(file,"%lld\n", n);
	for ( int i=1; i<=n; i++ ){
		fprintf(file,"%lld %lld\n", i, y[i] );
	}
	
	fclose(file);  // 关闭文件
	printf("文件5.in生成成功\n");
	
	/*-----------------------------------------------------------------------------------------*/
	//test case 5 output: 1e5 random numbers
	file = fopen("D:/AAAstuffsAAA/jmu-algo/exp01/p1/1-1_test_cases/5.out", "w");
	if (file == NULL) {
		printf("无法创建文件 5.out\n");
		return 1;
	}
	
	calculate_ans();
	fprintf(file,"%lld\n", ans);
	
	fclose(file);  // 关闭文件
	printf("文件5.out生成成功\n");
	/*-----------------------------------------------------------------------------------------*/
	//test case 6 input: 1e6 random numbers
	n = 1000000;
	for ( int i=1; i<=n; i++ )
		y[i] = rand()%(maxy-miny+1) + miny;
	
	file = fopen("D:/AAAstuffsAAA/jmu-algo/exp01/p1/1-1_test_cases/6.in", "w");
	if (file == NULL) {
		printf("无法创建文件 6.in\n");
		return 1;
	}
	
	fprintf(file,"%lld\n", n);
	for ( int i=1; i<=n; i++ ){
		fprintf(file,"%lld %lld\n", i, y[i] );
	}
	
	fclose(file);  // 关闭文件
	printf("文件6.in生成成功\n");
	
	/*-----------------------------------------------------------------------------------------*/
	//test case 6 output: 1e6 random numbers
	file = fopen("D:/AAAstuffsAAA/jmu-algo/exp01/p1/1-1_test_cases/6.out", "w");
	if (file == NULL) {
		printf("无法创建文件 6.out\n");
		return 1;
	}
	
	calculate_ans();
	fprintf(file,"%lld\n", ans);
	
	fclose(file);  // 关闭文件
	printf("文件6.out生成成功\n");
	/*-----------------------------------------------------------------------------------------*/
	//test case 7 input: 1e6 descending numbers
	n = 1000000;
	for ( int i=1; i<=n; i++ )
		y[i] = (n+1-i) % 10000;
	
	file = fopen("D:/AAAstuffsAAA/jmu-algo/exp01/p1/1-1_test_cases/7.in", "w");
	if (file == NULL) {
		printf("无法创建文件 7.in\n");
		return 1;
	}
	
	fprintf(file,"%lld\n", n);
	for ( int i=1; i<=n; i++ ){
		fprintf(file,"%lld %lld\n", i, y[i] );
	}
	
	fclose(file);  // 关闭文件
	printf("文件7.in生成成功\n");
	
	/*-----------------------------------------------------------------------------------------*/
	//test case 7 output: 1e6 descending numbers
	file = fopen("D:/AAAstuffsAAA/jmu-algo/exp01/p1/1-1_test_cases/7.out", "w");
	if (file == NULL) {
		printf("无法创建文件 7.out\n");
		return 1;
	}
	
	calculate_ans();
	fprintf(file,"%lld\n", ans);
	
	fclose(file);  // 关闭文件
	printf("文件7.out生成成功\n");
	/*-----------------------------------------------------------------------------------------*/
	//test case 8 input: 1e6 numbers that are mostly sorted
	n = 1000000;
	std::ranlux24 rng(SEED);
	for ( int i=1; i<=n; i++ )
		y[i] = rng()%(maxy-miny+1) + miny;
	//为了保证实现“1e6个基本有序的整数，先生成1e6个随机数，然后排序
	std::sort(y+1, y+1+n);
	
	//然后再随机选出1e2对 数字的pair，进行交换
	std::uniform_int_distribution<int> dist(1,n);
//	std::uniform_int_distribution 是 C++ 标准库中的一个类模板，用于生成均匀分布的整数。
//	<int> 是模板实参，指定了生成的整数的类型为 int。
//	dist 是创建的 std::uniform_int_distribution 对象的名称。
//	(x, y) 是分布函数对象的构造函数参数，指定了生成的整数的范围，闭区间 [x, y], 很显然这里的范围是数组下标范围， 所以是闭区间[1,n]
	int numSwaps = 111;
	for ( int i=1; i<=numSwaps; i++ ){
		int index1 = dist(rng);
		int index2 = dist(rng);
		std::swap(y[index1], y[index2]);
	}
	//"1e6个基本有序的随机数" 就完成了
	
	file = fopen("D:/AAAstuffsAAA/jmu-algo/exp01/p1/1-1_test_cases/8.in", "w");
	if (file == NULL) {
		printf("无法创建文件 8.in\n");
		return 1;
	}
	
	fprintf(file,"%lld\n", n);
	for ( int i=1; i<=n; i++ ){
		fprintf(file,"%lld %lld\n", i, y[i] );
	}
	
	fclose(file);  // 关闭文件
	printf("文件8.in生成成功\n");
	
	/*-----------------------------------------------------------------------------------------*/
	//test case 8 output: 1e8 numbers that are mostly sorted
	file = fopen("D:/AAAstuffsAAA/jmu-algo/exp01/p1/1-1_test_cases/8.out", "w");
	if (file == NULL) {
		printf("无法创建文件 8.out\n");
		return 1;
	}
	
	calculate_ans();
	fprintf(file,"%lld\n", ans);
	
	fclose(file);  // 关闭文件
	printf("文件8.out生成成功\n");
	/*-----------------------------------------------------------------------------------------*/
	
	return 0;
}
